# 爬取京东商城网址购物车中的所有商品信息图片、简介、颜色、尺码、价格

# 用requests请求网页 用pyquery解析数据 
from requests.exceptions import RequestException
import requests
from pyquery import PyQuery as pq
import time,json


def getPage(url):
    # 爬取页面
    try:
        # 封装头部信息
        headers = { 
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'max-age=0',
            'Connection': 'keep-alive',
            'Cookie': 'user-key=9253ebec-96e5-4bd9-91dd-cf13fad592bf; shshshfpa=bcc2d5c6-b171-e16e-1400-fb73e20b2347-1530327937; __jdc=122270672; __jdv=122270672|direct|-|none|-|1530327938112; __jdu=15303279381101856032071; shshshfpb=02777b76dc266fe2eb124d07217b64d4891ed9033e6c75b655b36f3821; 3AB9D23F7A4B3C9B=DK4ZDKF64SCLWO6XTE3U4RZORTQA23DG2S574MA646NZ7H5LK7OTIXWEUBX2HW4YUN5EQPY3CWHRDFIUTPX5DM7TP4; PCSYCityID=country_2468; ipLoc-djd=1-72-4137-0; areaId=1; cart-main=xx; shshshfp=7852e236ece7bb67037ed55c7d05bf72; cn=2; cd=0; __jda=122270672.15303279381101856032071.1530327938.1530344517.1530348443.4; shshshsID=a6c34ca48702eef03739124ee63d835e_2_1530348455756; __jdb=122270672.5.15303279381101856032071|4.1530348443; wlfstk_smdl=d2zbv0vtq7svsw8ygzpdb3q7rie0vcr8',
            'Host': 'cart.jd.com',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36',
        }
        res = requests.get(url,headers=headers)
        print(res.status_code)
        # print(res)
        if res.status_code == 200:
            return res.text
        else:
            return None
    except Exception as err:
        print(err)


def ParsePage(html):
    '''用pyquery解析页面'''
    doc = pq(html)
    items = doc('div.item-list')
    # print(len(items))
    for item in items.items():
        # print(item)
        yield {
            'image': item.find('div.p-img a img').attr('src'),
            'info': item.find('div.item-msg a').text(),
            'color': item.find('div.props-txt').text().split(' ')[0],
            'size': item.find('div.props-txt').text().split(' ')[1],
            'price': item.find('div.p-price-new strong').text(),
        }
def Writefile(content):
    with open("./jingdong.txt",'a',encoding='utf-8') as f:
        f.write(json.dumps(content,ensure_ascii=False)+"\n")

def main():
    '''函数调度'''
    url = 'https://cart.jd.com/cart.action'
    html = getPage(url) # 调取getpage对url地址进行爬取
    # print(len(html))
    # 判断是否爬取到数据，并调用解析函数
    if html:
        for item in ParsePage(html):
            print(item)
            Writefile(item)


if __name__ == '__main__':
    main()
    time.sleep(1)